import { getInfo, loginOut, getSipInfo } from '@/api/login'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { getAccessToken, removeToken } from '@/utils/auth'
import { defineStore } from 'pinia'
import { store } from '../index'
import { useCrmStore } from '@/store/modules/crm'
import { getLocalCache, resetLocalCache } from '@/utils/sortTable'
const { wsCache } = useCache()
const crmStore = useCrmStore()
interface UserVO {
  id: number
  avatar: string
  nickname: string
}
interface UserInfoVO {
  permissions: string[]
  modules: string[]
  roles: string[]
  isSetUser: boolean
  user: UserVO
}
export const useUserStore = defineStore('admin-user', {
  state: (): UserInfoVO => ({
    permissions: [],
    modules: [],
    roles: [],
    isSetUser: false,
    user: {
      id: 0,
      avatar: '',
      nickname: ''
    },
    sipMode: null,
    callChannel: [],
    newMsg: false //工作台新消息
  }),
  getters: {
    getPermissions(): string[] {
      return this.permissions
    },
    getModules(): string[] {
      return this.modules
    },
    getRoles(): string[] {
      return this.roles
    },
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getUser(): UserVO {
      return this.user
    },
    getCallChannel() {
      return this.callChannel
    },
    getSipMode() {
      return this.sipMode
    }
  },
  actions: {
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (!userInfo) {
        userInfo = await getInfo()
      }
      this.sipMode = await getSipInfo()
      this.permissions = userInfo.permissions
      if (userInfo.tenantInfo.authType == 2) {
        this.modules = ['ent']
      } else {
        this.modules = userInfo.modules // 后端返回的modules
      }
      this.roles = userInfo.roles
      this.user = userInfo.user
      this.callChannel = userInfo.callChannel
      this.isSetUser = true
      wsCache.set(CACHE_KEY.USER, userInfo)
      if (userInfo.tenantInfo.authType == 2) {
        const arr = userInfo.menus.filter((item) => item.path == '/ent')
        const arr2 = ['search', 'advanceSearch', 'receive-data']
        const childrens = arr[0].children.filter((item) => arr2.indexOf(item.path) != -1)
        arr[0].children = childrens
        wsCache.set(CACHE_KEY.ROLE_ROUTERS, arr)
      } else {
        wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
      }
      if (!sessionStorage.getItem('onces')) sessionStorage.setItem('onces', 1)
    },
    async loginOut() {
      sessionStorage.clear()
      await loginOut()
      // 防止有的浏览器无法删除， 只能使用set null 了
      wsCache.set('ACCESS_TOKEN', null)
      wsCache.set('REFRESH_TOKEN', null)
      removeToken()
      const localCache = getLocalCache()
      wsCache.clear()
      resetLocalCache(localCache)
      this.resetState()
      crmStore.resetState()
    },
    resetState() {
      this.permissions = []
      this.modules = []
      this.roles = []
      this.isSetUser = false
      this.user = {
        id: 0,
        avatar: '',
        nickname: ''
      }
      this.callChannel = []
    },
    updateNewMsg(value: Boolean) {
      this.newMsg = value
    }
  }
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
